/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.planning.tactical;

import java.beans.IntrospectionException;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import nl.cloudfarming.client.area.field.PartFieldLayer;
import nl.cloudfarming.client.area.field.PartFieldLayerObject;
import nl.cloudfarming.client.model.PartField;
import nl.cloudfarming.client.model.ProductionUnit;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;

/**
 * Factory for PartFieldNode
 * @author Merijn Zengers
 */
public class PartFieldNodeFactory extends ChildFactory<PartField> implements PropertyChangeListener{
    private TacticalPlanningService service;
    private final PartFieldLayer layer;
    private final ProductionUnit productionUnit;
    
    public PartFieldNodeFactory(ProductionUnit productionUnit, PartFieldLayer layer, TacticalPlanningService service){
        this.productionUnit = productionUnit;
        this.layer = layer;
        this.service = service;
        init();
    }
    
    public final void init(){
        service.addPropertyChangeListener(TacticalPlanningService.PART_FIELD_LINKED, this);
    }
    
    /**
     * Creates Node based on bean. Name is field.node.name + id
     * @param key PartField which is wrapped in this node 
     * @return PartFieldNode which is created for this PartField
     */
    @Override
    protected Node createNodeForKey(PartField key) {
        try {
            StringBuilder nameBuilder = new StringBuilder(NbBundle.getMessage(TacticalPlanningModule.class, "field.node.name"));
            nameBuilder.append("-");
            nameBuilder.append(key.getId());
            key.setName(nameBuilder.toString());
            PartFieldLayerObject layerObject = new PartFieldLayerObject(key);
            layer.addObject(layerObject);
            return new PartFieldNode(layer, layerObject, service);
        } catch (IntrospectionException ex) {
            Exceptions.printStackTrace(ex);
            return null;
        }
    }

    /**
     * Get all the keys based on all the available partFields in productionUnit
     * @param toPopulate List of partFields to which the new partfiels are added 
     * @return always true
     */
    @Override
    protected boolean createKeys(List<PartField> toPopulate) {
        toPopulate.addAll(productionUnit.getPartFields());
        return true;
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        refresh(true);
    }
    
}
